import service from "./service";
import { useUserStoreWithOut } from "@/store/modules/user";
import { CONTENT_TYPE, HEADERS, METHOD } from "./constants";
const request = (option: AxiosConfig) => {
  const { url, method, params, data, headers, responseType } = option;
  const userStore = useUserStoreWithOut();
  return service.request({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    headers: {
      [HEADERS.CONTENT_TYPE]: CONTENT_TYPE.JSON,
      [HEADERS.AUTHORIZATION]: userStore.getToken ?? "",
      ...headers,
    },
  });
};

export default {
  get: <T = any>(option: AxiosConfig) => {
    return request({ method: METHOD.GET, ...option }) as Promise<IResponse<T>>;
  },
  post: <T = any>(option: AxiosConfig) => {
    return request({ method: METHOD.POST, ...option }) as Promise<IResponse<T>>;
  },
  formdata: <T = any>(option: AxiosConfig) => {
    return request({
      method: METHOD.POST,
      headers: { [HEADERS.CONTENT_TYPE]: CONTENT_TYPE.FORM_DATA },
      ...option,
    }) as Promise<IResponse<T>>;
  },
};
